package com.cheng.reggie.controller;

import com.cheng.reggie.common.R;
import com.cheng.reggie.dto.DishDto;
import com.cheng.reggie.entity.Dish;
import com.cheng.reggie.entity.Page;
import com.cheng.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired(required = false)
    private DishService dishService;

    @RequestMapping("/list")
    public R<List<DishDto>> findByCategoryId(Long categoryId,Integer status){
        //调用业务查询类别id的菜品列表
        List<DishDto> dishList = dishService.findByCategoryId(categoryId,status);
        //封装数据返回
        return R.success(dishList);
    }


    @PutMapping
    public R<String> updateById(@RequestBody DishDto dishDto,HttpSession session){
        //获取登陆者id
        Long id = (Long) session.getAttribute("employee");
        //完善信息
        dishDto.setUpdateUser(id);
        dishService.updateById(dishDto);
        return R.success("修改菜品成功!");
    }


    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        //调用service查询一个菜品多个口味
        DishDto byId = dishService.findById(id);
        return R.success(byId);

    }

    @PostMapping
    public R<String> sava(@RequestBody DishDto dishDto, HttpSession session){
        //获取登录人id
        Long id = (Long) session.getAttribute("employee");
        //完善数据修改人,创建人
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);

        //调用业务添加
        dishService.save(dishDto);
        return R.success("添加成功!");
    }
    
    //分类列表查询
    @GetMapping("/page")
    public R<Page<DishDto>> findByPage2(Integer page, Integer pageSize, String name){
        //调用service分页方法查询数据
        Page<DishDto> pageObj = dishService.findByPage2(page, pageSize, name);
        return R.success(pageObj);

    }
}
